<?php

class ActionHelper_Abstract_Basic extends Zend_Controller_Action_Helper_Abstract {
	
	protected $_i18nMsg;
	
	protected $_i18nMsgTranslateArray;
	
	protected $view;
	/**
	 * System pararm
	 * @var Object_User
	 */
	protected $_loginUser;
	/**
	 * @var Module_Interface_Translate
	 */
	protected $_translator;
	
	protected $_defaultData;
	
	protected $_formData;
	/**
	 * @var Zend_Form
	 * @access protected
	 */
	protected $_form;
	
	/**
	 * System pararm
	 * @var Object_PageInfo
	 */
	protected $_pageInfo;
	protected $_pageUrl;
	
	protected function _changeI18nTranslate() {
		$options = null;
		if (! empty ( $this->_i18nMsgTranslateArray )) {
			foreach ( $this->_i18nMsgTranslateArray as $key => $value ) {
				if (is_array ( $this->_defaultData ) and isset ( $this->_defaultData [$value] )) {
					$options [$key] = $this->_defaultData [$value];
				}elseif(is_array ( $this->_formData ) and isset ( $this->_formData  [$value] )){
					$options [$key] = $this->_formData [$value];
				}
			}
		}
		return $options;
	}	
	
	public function init() {
		$this->view = $this->getActionController ()->view;
		$this->_translator = Module_Impl_I18n::getInstance();
		$this->_loginUser = Zend_Registry::get('loginUser');	
		$this->_pageInfo = Zend_Registry::get ( 'pageInfo' );
		$this->_pageUrl = $this->_pageInfo->getCurrentUrl();	

		//$module = $this->getActionController()->getRequest()->getModuleName();
		$controller = $this->getActionController ()->getRequest ()->getControllerName ();
		$action = $this->getActionController ()->getRequest ()->getActionName ();
		$str = $controller . '.' . $action;
		$helperI18nMsg [self::TITLE] = $str . '.title';
		$helperI18nMsg [self::INPUT] = $str . '.input';
		$helperI18nMsg [self::SUCCESS] = $str . '.success';
		$helperI18nMsg [self::SUCCESS_DELETE] = $str . '.delete.success';
		$helperI18nMsg [self::SUCCESS_UPDATE] = $str . '.update.success';
		$helperI18nMsg [self::SUCCESS_REGISTER] = $str . '.register.success';
		$helperI18nMsg [self::SUCCESS_ORDER] = $str . '.order.success';
		$helperI18nMsg [self::FAIL] = $str . '.fail';
		$helperI18nMsg [self::FAIL_DELETE] = $str . '.delete.fail';
		$helperI18nMsg [self::FAIL_UPDATE] = $str . '.update.fail';
		$helperI18nMsg [self::FAIL_REGISTER] = $str . '.register.fail';
		$helperI18nMsg [self::FAIL_ORDER] = $str . '.order.fail';
		$helperI18nMsg [self::NO_ID] = $str . '.fail.id.no.exist';
		$helperI18nMsg [self::NO_EXIST] = $str . '.fail.no.exist';
		$helperI18nMsg [self::CANNOT_EDIT] = $str . '.fail.cannot.edit';
		$helperI18nMsg [self::NO_DATA] = $str . '.no.data';
		$this->_i18nMsg = $helperI18nMsg ;	
	}
	
	public function _($string, $options = null) {		
		return $this->_translator->_($string,$options);
	}
	
	const TITLE = 'title';
	const INPUT = 'input';
	const SUCCESS = 'success';
	const SUCCESS_DELETE = 'delete.success';
	const SUCCESS_UPDATE = 'update.success';
	const SUCCESS_REGISTER = 'register.success';
	const SUCCESS_ORDER = 'order.success';
	const FAIL = 'fail';
	const FAIL_DELETE = 'delete.fail';
	const FAIL_UPDATE = 'update.fail';
	const FAIL_REGISTER = 'register.fail';
	
	const FAIL_ORDER = 'order.fail';
	
	const NO_ID = 'no_id';
	const CANNOT_EDIT = 'cannot_edit';
	const NO_EXIST = 'no_exist';
	const NO_DATA = 'no_data';
	
	
	
	const OPTIONS_FORM = 'form';
	const OPTIONS_I18NMSG = 'i18nmsg';
	const OPTIONS_I18NMSG_TRANSLATEARRAY = 'i18nmsg_translateArray';
	const OPTIONS_PAGEURL = 'pageurl';
	
	public function setOptions($options){
		if(isset($options[self::OPTIONS_FORM])){
			$this->_form = $options[self::OPTIONS_FORM];
			$this->view->form = $this->_form;
		}
		if(isset($options[self::OPTIONS_I18NMSG])){
			$this->_i18nMsg = array_merge($this->_i18nMsg,$options[self::OPTIONS_I18NMSG]);
		}
		if(isset($options[self::OPTIONS_I18NMSG_TRANSLATEARRAY])){
			$this->_i18nMsgTranslateArray = $options[self::OPTIONS_I18NMSG_TRANSLATEARRAY];
		}
		if(isset($options[self::OPTIONS_PAGEURL])){
			$this->_pageUrl = $options[self::OPTIONS_PAGEURL];
		}
	}
	
	public function process($options = null) {
		if (! empty ( $options ) and is_array ( $options )) {
			foreach ( $options as $key => $option ) {
				$this->$key = $option;
			}
		}
		$this->view->title = $this->_ ( $this->_i18nMsg [self::TITLE] );
		$this->_formData = null;
	}

}